﻿using System.Collections.Generic;
using System.ComponentModel;
using Dataweb.NShape.Advanced;
using Dataweb.NShape;
using cz.zcu.PreSti.Components;
using System.Drawing;
using System;

namespace cz.zcu.PreSti.Shapes
{
    class UniformDistributionGeneratorSymbol : GeneratorSymbol
    {
        #region ISymbol Members

        public override BaseComponent initializeComponent(IPresentation p)
        {
            UniformDistributionGeneratorModel model = (UniformDistributionGeneratorModel)this.ModelObject;
            UniformDistributionGenerator generator = new UniformDistributionGenerator();
            generator.StartInterval = model.StartInterval;
            generator.EndInterval = model.EndInterval;
            return generator;
        }

        #endregion

        protected internal UniformDistributionGeneratorSymbol(ShapeType shapeType, Template template)
            : base(shapeType, template)
        {
        }


        protected internal UniformDistributionGeneratorSymbol(ShapeType shapeType, IStyleSet styleSet)
            : base(shapeType, styleSet)
        {
        }

        public override Shape Clone()
        {
            Shape result = new UniformDistributionGeneratorSymbol(Type, (Template)null);
            result.CopyFrom(this);
            return result;
        }

        public override void CopyFrom(Shape source)
        {
            base.CopyFrom(source);
        }

        protected override void DrawIcon(Graphics graphics)
        {
            graphics.DrawImage(icons.uniform, new Rectangle(X - Diameter / 2, Y - Diameter / 2, Diameter, Diameter));
        }

        internal static Shape CreateInstance(ShapeType shapeType, Template template)
        {
            return new UniformDistributionGeneratorSymbol(shapeType, template);
        }
    }

    class UniformDistributionGeneratorModel : ModelObjectBase
    {

        #region Fields

        private double startInterval;
        private double endInterval;
        private char securityDomainName = 'A';

        private const int PropertyIdStartInterval = 1;
        private const int PropertyIdEndInterval = 2;

        #endregion

        #region properties

        [Category("Presentation")]
        [Description("Start of the interval for the uniform distribution generator")]
        [PropertyMappingId(PropertyIdStartInterval)]
        public virtual double StartInterval
        {
            get
            {
                return startInterval;
            }
            set
            {
                startInterval = value;
            }
        }

        [Category("Presentation")]
        [Description("End of the interval for the uniform distribution generator")]
        [PropertyMappingId(PropertyIdEndInterval)]
        public virtual double EndInterval
        {
            get
            {
                return endInterval;
            }
            set
            {
                endInterval = value;
            }
        }

        #endregion

        public static UniformDistributionGeneratorModel CreateInstance(ModelObjectType modelObjectType) {
			return new UniformDistributionGeneratorModel(modelObjectType);
		}


		protected internal UniformDistributionGeneratorModel(ModelObjectType modelObjectType)
			: base(modelObjectType)
        {
            this.terminalCount = 1;
		}


        protected internal UniformDistributionGeneratorModel(UniformDistributionGeneratorModel source)
			: base(source) {
            StartInterval = source.StartInterval;
            EndInterval = source.EndInterval;
            this.terminalCount = 1;
		}


		public override IModelObject Clone() {
            return new UniformDistributionGeneratorModel(this);
		}


		public override IEnumerable<MenuItemDef> GetMenuItemDefs() {
			throw new NotImplementedException();
		}


		public override void Connect(TerminalId ownTerminalId, IModelObject targetConnector, TerminalId targetTerminalId) {
			throw new NotImplementedException();
		}


		public override void Disconnect(TerminalId ownTerminalId, IModelObject targetConnector, TerminalId targetTerminalId) {
			throw new NotImplementedException();
		}

        public override char SecurityDomainName
        {
            get { return securityDomainName; }
            set
            {
                if (value < 'A' || value > 'Z')
                    throw new ArgumentOutOfRangeException("SecurityDomainName", "The domain qualifier has to be an upper case  ANSI letter (A-Z).");
                securityDomainName = value;
            }
        }

        #region IEntity Members

        public static new IEnumerable<EntityPropertyDefinition> GetPropertyDefinitions(int version)
        {
            foreach (EntityPropertyDefinition pi in ModelObjectBase.GetPropertyDefinitions(version))
                yield return pi;
            yield return new EntityFieldDefinition("StartInterval", typeof(double));
            yield return new EntityFieldDefinition("EndInterval", typeof(double));
        }

        protected override void LoadFieldsCore(IRepositoryReader reader, int version)
        {
            base.LoadFieldsCore(reader, version);
            StartInterval = reader.ReadDouble();
            EndInterval = reader.ReadDouble();
        }

        protected override void SaveFieldsCore(IRepositoryWriter writer, int version)
        {
            base.SaveFieldsCore(writer, version);
            writer.WriteDouble(StartInterval);
            writer.WriteDouble(EndInterval);
        }

        #endregion

    }
}
